package com.mmm.service;

import com.mmm.dao.UserMapper;
import com.mmm.exception.AccountOverDrawnException;
import com.mmm.exception.invalidDepositException;
import com.mmm.pojo.Users;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author mmm
 * @date 2021/9/22 - 16:49
 */
//@Service
//@Transactional
public class UserServiceImpl implements UserService {
//     @Autowired
    private UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper=userMapper;
    }

    @Override
    public double inquiry(int userId) {
       Users users=userMapper.selectUserByUserId(userId);
        return users.getBalance();
    }
@Transactional()
    @Override
    public int deposit(String userName, double balance) throws invalidDepositException {
        if(balance<=0){
            throw new invalidDepositException("存款金额不能小于0");
        }
        double current=userMapper.selectUserByUserName(userName).getBalance();
        int status=userMapper.updateMoney(userName,current+balance);
        return status;
    }
@Transactional()
    @Override
    public int withdrawals(String userName, double balance) throws AccountOverDrawnException {
     Users users=userMapper.selectUserByUserName(userName);
      if (users==null){
         return 0;
     }else if (balance<=0){
           return 0;
        }
        double current=userMapper.selectUserByUserName(userName).getBalance();
        if (current<balance){
            return 0;
        }
        int status=userMapper.updateMoney(userName,current-balance);
        return status;
    }

    @Override
    public Users login(String userName, String password) {
        Users users=userMapper.selectUser(userName,password);

        return users;
    }

    @Override
    public boolean register(String userName,String password,double balance) {
        Users users=userMapper.selectUserByUserName(userName);
        if (users==null){
            userMapper.insertUser(userName,password,balance);
            return true;
        }
        System.out.println("用户已存在");
        return false;
    }

    @Transactional()
    @Override
    public int Transfer(String userName, double balance, String transferName) {
       Users users1=userMapper.selectUserByUserName(userName);
       Users users2=userMapper.selectUserByUserName(transferName);
        if (users1==null||users2==null){
            return 0;
        }else if (balance<0){
            return 0;
        }else if (users1.getBalance()<balance){
            return  0;
        }
        if (users1.getUserFlag()==0||users2.getUserFlag()==0){
            return 0;
        }
        if (userName.equals(transferName)){
            return  0;
        }
        userMapper.updateMoney(userName,users1.getBalance()-balance);
       int status= userMapper.updateMoney(transferName,users2.getBalance()+balance);
        return status;
    }

    @Override
    public void exitSystem() {
        System.exit(1);
    }

    @Override
    public int frozenAccount(int userId) {
          return  userMapper.updateUserFlag(userId);
        }



    @Override
    public int deleteUser(int userId) {
        int status=userMapper.deleteUser(userId);
        return status;
    }

    @Override
    public List<Users> findAllUser() {
        return userMapper.selectAllUser();
    }

    @Override
    public Users selectUserByUserName(String userName) {
        return userMapper.selectUserByUserName(userName);
    }


}

